/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 徐海涛
 * Date: 2021-11-29
 * Time: 9:10
 */
public class TestDemo {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode pl = headA;
        ListNode ps = headB;
        int lenA = 0;
        int lebB = 0;
        while (pl != null) {
            pl = pl.next;
            lenA++;
            pl = headA;
        }
        while (ps != null) {
            ps = ps.next;
            lebB++;
            ps = headB;
        }
        int len = lenA - lebB;
        if(len < 0) {
            pl = headB;
            ps = headA;
        }
        while (len != 0) {
            pl = pl.next;
            len--;
        }
        while (pl != ps) {
            pl = pl.next;
            ps = ps.next;
        }
        return pl;
    }

























    public static ListNode mergeTwoLists(ListNode headA, ListNode headB){
        ListNode newHead = new ListNode(-1);//创建一个虚拟节点
        ListNode tmp = newHead;
        while (headA!=null && headB!=null) {
            if(headA.val < headB.val) {
                tmp.next = headA;
                headA = headA.next;
                tmp = tmp.next;
            }else {
                tmp.next =headB;
                headB = headB.next;
                tmp = tmp.next;
            }
            if(headA!=null) {
                return headA;
            }
            if(headB!=null) {
                return headB;
            }
        }
        return newHead;
    }



































    public static ListNode mergeTwoLists1(ListNode headA, ListNode headB) {
        ListNode newHead = new ListNode(-1);//创建个虚拟节点newHead。
        ListNode tmp = newHead;//定义一个tmp指向虚拟节点
        while (headA != null && headB != null) {
            if (headA.val < headB.val) {
                tmp.next = headA;
                headA = headA.next;//往下走
                tmp = tmp.next;//往下走
            } else {
                tmp.next = headB;
                headB = headB.next;
                tmp = tmp.next;
            }
            if (headA != null) {//判断一下哪里走完
                tmp.next = headA;
            }
            if (headB != null) {
                tmp.next = headB;
            }
        }
        return newHead.next;
    }

    public static void main(String[] args) {
        MyLinkedList myLinkedList = new MyLinkedList();
        //myLinkedList.createList();
        myLinkedList.addFirst(12);//头插法打印结果
        myLinkedList.addFirst(23);
        myLinkedList.addFirst(34);
        myLinkedList.addFirst(45);


        MyLinkedList myLinkedList2 = new MyLinkedList();

        myLinkedList2.addFirst(13);//头插法打印结果
        myLinkedList2.addFirst(24);
        myLinkedList2.addFirst(30);
        ListNode ret = mergeTwoLists1(myLinkedList.head,myLinkedList2.head);
        myLinkedList.display();


    }
    public static void main1(String[] args) {
        MyLinkedList myLinkedList = new MyLinkedList();
        //myLinkedList.createList();
        myLinkedList.addFirst(1);//头插法打印结果
        myLinkedList.addFirst(2);
        myLinkedList.addFirst(3);
        myLinkedList.addFirst(4);
        myLinkedList.addLast(1);//尾插
        myLinkedList.addIndex(3, 99);
        myLinkedList.display();
        boolean flg = myLinkedList.contains(134);
        System.out.println(flg);
        System.out.println(myLinkedList.size());//链表长度
    }
}

